package modelo;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import dao.PersonaDao;
import excepciones.DaoException;

@FacesConverter( value="personaConverter" )
public class PersonaConverter implements Converter{

	public String getAsString(FacesContext context, UIComponent component, Object value) {
        // Convert the Foo object to its unique String representation.
        return (value instanceof Persona) ? ((Persona) value).getApellido() + ", " + ((Persona) value).getNombre() : null;

    }

	@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
		
		PersonaDao personaDao = new PersonaDao();
		Persona persona = null;
		try {
			persona = (Persona) personaDao.encontrarPorPersona(value);
			personaDao.closeEntityManager();
		} catch (DaoException e) {			
			e.printStackTrace();
		}
		
		//System.out.println(value);
        return persona;
	}

}
